<template>
  <div class="app-container">
    <vue2-org-tree
      :data="data"
      :horizontal="horizontal"
      :collapsable="collapsable"
      :label-class-name="labelClassName"
      :render-content="renderContent"
      @on-expand="onExpand"
      @on-node-click="onNodeClick"
    />
    <el-dialog title="知识点" :visible.sync="nodeClickShow" width="30%">
      <span>当前节点</span>

      <el-divider></el-divider>
      <el-input type="text" v-model="currentNode.name" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="nodeClickShow = false">取 消</el-button>
        <el-button @click="updateKnowledgePoints()">修 改</el-button>
        <el-button @click="deleteKnowledgePoints()">删 除</el-button>
        <el-button
          type="primary"
          @click="
            nodeClickShow = false;
            nodeChildShow = true;
          "
          >添加子节点</el-button
        >
      </span>
    </el-dialog>
    <el-dialog title="添加子节点" :visible.sync="nodeChildShow" width="30%">
      <el-input type="text" v-model="currentChildNode.name" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="nodeChildShow = false">取 消</el-button>
        <el-button type="primary" @click="saveChildKnowledgeNode()"
          >保存</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getKnowledgeNode,
  updateKnowledgeNode,
  createKnowledgeNode,
  deleteKnowledgeNode,
} from "@/api/course";
export default {
  name: "index",
  data() {
    return {
      data: {
        id: 0,
        name: this.$route.query.courseName,
        children: [],
      },
      courseId: 0,
      nodeClickShow: false,
      nodeChildShow: false,
      currentNode: {},
      currentChildNode: {},
      horizontal: true,
      collapsable: false,
      expandAll: true,
      labelClassName: "bg-orange",
    };
  },
  mounted: function () {
    //获取当前课程的知识点

    this.courseId = this.$route.query.courseId;
    this.getKnowledgeNodeFunction();
  },
  methods: {
    formatting(){
      this.currentChildNode = {
        name:""
      }
    },
    deleteKnowledgePoints: function () {
      //顶级目录不能删除，
      if (this.currentNode.children != undefined) {
        if (this.currentNode.parentId == 0) {
          alert("顶级节点不能删除");
          return;
        }

        //判断是否有子节点
        if (this.currentNode.children.length != 0) {
          alert("请先删除子知识点");
          return;
        }
      }

      if (
        this.currentNode.parentId != 0
      ) {
        //alert("可以删除");
        deleteKnowledgeNode(this.currentNode.id).then(response=>{
          this.nodeClickShow = false;
          this.getKnowledgeNodeFunction();
        });
      }
    },
    updateKnowledgePoints() {
      updateKnowledgeNode(this.currentNode).then((response) => {
        this.nodeClickShow = false;
        this.getKnowledgeNodeFunction();
      });
    },
    getKnowledgeNodeFunction: function () {
      let loadingInstance = this.$loading({
        fullscreen: true,
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      let data = {
        courseId: this.courseId,
      };
      getKnowledgeNode(data)
        .then((response) => {
          const { data } = response;
          this.data.children = this.handleTree(data, "id");
          loadingInstance.close();
        })
        .catch((error) => {
          this.$message("加载失败，请刷新重试");
          loadingInstance.close();
        });
    },
    saveChildKnowledgeNode: function () {
      let data = {
        parentId: this.currentNode.id,
        name: this.currentChildNode.name,
        courseId: this.courseId,
      };
      createKnowledgeNode(data).then((response) => {
        //console.log(response);
        this.formatting();
        this.getKnowledgeNodeFunction();
        this.nodeChildShow = false;
      });
    },
    renderContent: function (h, data) {
      return data.name;
    },
    onExpand: function (data) {
      if ("expand" in data) {
        data.expand = !data.expand;

        if (!data.expand && data.children) {
          this.collapse(data.children);
        }
      } else {
        this.$set(data, "expand", true);
      }
    },
    onNodeClick: function (e, data) {
      //alert(data.label);
      this.currentNode = data;
      this.nodeClickShow = true;
    },
    collapse: function (list) {
      var _this = this;
      list.forEach(function (child) {
        if (child.expand) {
          child.expand = false;
        }

        child.children && _this.collapse(child.children);
      });
    },
    expandChange: function () {
      this.toggleExpand(this.data, this.expandAll);
    },
    toggleExpand: function (data, val) {
      var _this = this;
      if (Array.isArray(data)) {
        data.forEach(function (item) {
          _this.$set(item, "expand", val);
          if (item.children) {
            _this.toggleExpand(item.children, val);
          }
        });
      } else {
        this.$set(data, "expand", val);
        if (data.children) {
          _this.toggleExpand(data.children, val);
        }
      }
    },
  },
};
</script>

<style lang="less">
@colors: #cccccc;
.org-tree-container {
  display: inline-block;
  padding: 15px;
  // margin: 0 auto;

  // position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  margin-top: 50px;
}

.org-tree {
  // display: inline-block;
  display: table;
  text-align: center;

  &:before,
  &:after {
    content: "";
    display: table;
  }

  &:after {
    clear: both;
  }
}

.org-tree-node,
.org-tree-node-children {
  position: relative;
  margin: 0;
  padding: 0;
  list-style-type: none;

  &:before,
  &:after {
    transition: all 0.5s;
  }
}
.org-tree-node-label {
  position: relative;
  display: inline-block;

  .org-tree-node-label-inner {
    cursor: pointer;
    padding: 8px;
    text-align: center;
    border-radius: 3px;
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.7);
    font-size: 13px;
  }
}
.org-tree-node-btn {
  position: absolute;
  top: 100%;
  left: 50%;
  width: 20px;
  height: 20px;
  z-index: 10;
  margin-left: -11px;
  margin-top: 9px;
  background-color: #fff;
  border: 1px solid @colors;
  border-radius: 50%;
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  transition: all 0.35s ease;

  &:hover {
    background-color: #e7e8e9;
    transform: scale(1.15);
  }

  &:before,
  &:after {
    content: "";
    position: absolute;
  }

  &:before {
    top: 50%;
    left: 4px;
    right: 4px;
    height: 0;
    border-top: 1px solid @colors;
  }

  &:after {
    top: 4px;
    left: 50%;
    bottom: 4px;
    width: 0;
    border-left: 1px solid @colors;
  }

  &.expanded:after {
    border: none;
  }
}
.org-tree-node {
  padding-top: 50px;
  display: table-cell;
  vertical-align: top;
  position: relative;

  &.is-leaf,
  &.collapsed {
    padding-left: 10px;
    padding-right: 10px;
  }

  &:before,
  &:after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    height: 50px;
  }

  &:after {
    left: 50%;
    border-left: 7px solid @colors;
    // background: url('../image/jt.png') 0 0 no-repeat;
    // background-size: 100% 100%;
    // background-position-x: -25px;
  }

  &:not(:first-child):before,
  &:not(:last-child):after {
    border-top: 7px solid @colors;
  }
}
.collapsable .org-tree-node.collapsed {
  padding-bottom: 30px;

  .org-tree-node-label:after {
    content: "";
    position: absolute;
    top: 100%;
    left: 0;
    width: 50%;
    height: 20px;
    border-right: 1px solid @colors;
  }
}
.org-tree > .org-tree-node {
  padding-top: 0;

  &:after {
    border-left: 0;
  }
}
.org-tree-node-children {
  padding-top: 50px;
  display: table;

  &:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    height: 50px;
    border-right: 7px solid @colors;
    border-left: none;
  }

  &:after {
    content: "";
    display: table;
    clear: both;
  }
}

.horizontal {
  .org-tree-node {
    // display: flex;
    // flex-direction: row;
    // justify-content: flex-start;
    // align-items: center;
    display: table-cell;
    float: none;
    padding-top: 0;
    padding-left: 20px;

    &.is-leaf,
    &.collapsed {
      padding-top: 5px;
      padding-bottom: 5px;
    }

    &:before,
    &:after {
      width: 19px;
      height: 50%;
    }

    &:after {
      top: 50%;
      left: 0;
      border-left: 0;
    }

    &:only-child:before {
      top: 1px;
      border-bottom: 1px solid @colors;
    }

    &:not(:first-child):before,
    &:not(:last-child):after {
      border-top: 0;
      border-left: 1px solid @colors;
    }

    &:not(:only-child):after {
      border-top: 1px solid @colors;
    }

    .org-tree-node-inner {
      display: table;
    }
  }

  .org-tree-node-label {
    display: table-cell;
    vertical-align: middle;
  }

  &.collapsable .org-tree-node.collapsed {
    padding-right: 30px;

    .org-tree-node-label:after {
      top: 0;
      left: 100%;
      width: 20px;
      height: 50%;
      border-right: 0;
      border-bottom: 1px solid @colors;
    }
  }

  .org-tree-node-btn {
    top: 50%;
    left: 100%;
    margin-top: -11px;
    margin-left: 9px;
  }

  & > .org-tree-node:only-child:before {
    border-bottom: 0;
  }

  .org-tree-node-children {
    // display: flex;
    // flex-direction: column;
    // justify-content: center;
    // align-items: flex-start;
    display: table-cell;
    padding-top: 0;
    padding-left: 20px;

    &:before {
      top: 50%;
      left: 0;
      width: 20px;
      height: 0;
      border-left: 0;
      border-top: 1px solid @colors;
    }

    &:after {
      display: none;
    }

    & > .org-tree-node {
      display: block;
    }
  }
}
</style>
